package com.devbobcorn.nekoration.client.rendering.entities;

import com.devbobcorn.nekoration.entities.ElectricArcEntity;
import com.mojang.blaze3d.matrix.MatrixStack;
import com.mojang.blaze3d.vertex.IVertexBuilder;
import java.util.Random;
import net.minecraft.client.renderer.IRenderTypeBuffer;
import net.minecraft.client.renderer.RenderType;
import net.minecraft.client.renderer.entity.EntityRenderer;
import net.minecraft.client.renderer.entity.EntityRendererManager;
import net.minecraft.client.renderer.texture.AtlasTexture;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.math.vector.Matrix4f;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;

@OnlyIn(Dist.CLIENT)
public class ElectricArcRenderer extends EntityRenderer<ElectricArcEntity> {
	public ElectricArcRenderer(EntityRendererManager renderer) {
		super(renderer);
	}

	public void render(ElectricArcEntity entity, float rotation, float partialTicks, MatrixStack stack, IRenderTypeBuffer buffers, int packedLight) {
		float[] afloat = new float[8];
		float[] afloat1 = new float[8];
		float f = 0.0F;
		float f1 = 0.0F;
		Random random = new Random(entity.seed);

		for (int i = 7; i >= 0; --i) {
			afloat[i] = f;
			afloat1[i] = f1;
			f += (float) (random.nextInt(11) - 5);
			f1 += (float) (random.nextInt(11) - 5);
		}
		IVertexBuilder ivertexbuilder = buffers.getBuffer(RenderType.lightning());
		Matrix4f matrix4f = stack.last().pose();
		for (int j = 0; j < 4; ++j) {
			Random random1 = new Random(entity.seed);
			for (int k = 0; k < 3; ++k) {
				int l = 7;
				int i1 = 0;
				if (k > 0) {
					l = 7 - k;
				}
				if (k > 0) {
					i1 = l - 2;
				}
				float f2 = afloat[l] - f;
				float f3 = afloat1[l] - f1;
				for (int j1 = l; j1 >= i1; --j1) {
					float f4 = f2;
					float f5 = f3;
					if (k == 0) {
						f2 += (float) (random1.nextInt(11) - 5);
						f3 += (float) (random1.nextInt(11) - 5);
					} else {
						f2 += (float) (random1.nextInt(31) - 15);
						f3 += (float) (random1.nextInt(31) - 15);
					}
					float f10 = 0.1F + (float) j * 0.2F;
					if (k == 0) {
						f10 = (float) ((double) f10 * ((double) j1 * 0.1D + 1.0D));
					}
					float f11 = 0.1F + (float) j * 0.2F;
					if (k == 0) {
						f11 *= (float) (j1 - 1) * 0.1F + 1.0F;
					}
					quad(matrix4f, ivertexbuilder, f2, f3, j1, f4, f5, 0.45F, 0.45F, 0.5F, f10, f11, false, false,  true, false);
					quad(matrix4f, ivertexbuilder, f2, f3, j1, f4, f5, 0.45F, 0.45F, 0.5F, f10, f11,  true, false,  true,  true);
					quad(matrix4f, ivertexbuilder, f2, f3, j1, f4, f5, 0.45F, 0.45F, 0.5F, f10, f11,  true,  true, false,  true);
					quad(matrix4f, ivertexbuilder, f2, f3, j1, f4, f5, 0.45F, 0.45F, 0.5F, f10, f11, false,  true, false, false);
				}
			}
		}
	}

	private static void quad(Matrix4f pose, IVertexBuilder vertex, float n1, float n2, int hght, float n3, float n4, float r, float g, float b, float d1, float d2, boolean f1, boolean f2, boolean f3, boolean f4) {
		vertex.vertex(pose, n1 + (f1 ? d2 : -d2), (float) (hght * 16),       n2 + (f2 ? d2 : -d2)).color(r, g, b, 0.3F).endVertex();
		vertex.vertex(pose, n3 + (f1 ? d1 : -d1), (float) ((hght + 1) * 16), n4 + (f2 ? d1 : -d1)).color(r, g, b, 0.3F).endVertex();
		vertex.vertex(pose, n3 + (f3 ? d1 : -d1), (float) ((hght + 1) * 16), n4 + (f4 ? d1 : -d1)).color(r, g, b, 0.3F).endVertex();
		vertex.vertex(pose, n1 + (f3 ? d2 : -d2), (float) (hght * 16),       n2 + (f4 ? d2 : -d2)).color(r, g, b, 0.3F).endVertex();
	}

	@SuppressWarnings("deprecation")
	public ResourceLocation getTextureLocation(ElectricArcEntity entity) {
		return AtlasTexture.LOCATION_BLOCKS;
	}
}